<template>
	<!-- 修改邮箱 -->
	<view>
		<baseHead :title="title" :showReturn="true" :right="false"></baseHead>
		<view class="box">
			<u-form :model="form" label-width="100" prop="form" borderBottom required :borderBottom="false">
				<u-form-item label="原邮箱" label-width="100" class="content" v-if="type">
					<view style="width: 100%">
						<u-input style="background-color: #fff" placeholder="请输入原邮箱" v-model="form.oldEmail"
							input-align="right" border="none" :adjustPosition="false" />
					</view>
				</u-form-item>

				<u-form-item label="新邮箱" label-width="100" class="content">
					<view style="width: 100%">
						<u-input style="background-color: #fff" placeholder="请输入新邮箱" v-model="form.Email"
							input-align="right" border="none" :adjustPosition="false" />
					</view>
				</u-form-item>

				<u-form-item label="验证码" label-width="100" prop="from.Code" borderBottom ref="item1"
					:borderBottom="false" class="content">
					<view style="width: 100%">
						<!-- #ifndef APP-NVUE -->
						<u-input v-model="form.Code" border="none" placeholder="请输入验证码" :adjustPosition="false">
							<!-- #endif -->
							<!-- #ifdef APP-NVUE -->
							<u--input v-model="form.Code" border="none" placeholder="请输入验证码" :adjustPosition='false'>
								<!-- #endif -->
								<template slot="suffix">
									<u-button @tap="yanzhengma" :text="tips" type="success" size="mini"
										:disabled="disabled1"></u-button>
								</template>
								<!-- #ifndef APP-NVUE -->
						</u-input>
						<!-- #endif -->
						<!-- #ifdef APP-NVUE -->
						</u--input>
						<!-- #endif -->
					</view>
				</u-form-item>
				<u-code ref="uCode" @change="codeChange" seconds="60" @start="disabled1 = true"
					@end="disabled1 = false">
				</u-code>
			</u-form>

		</view>
		<view class="btn" @click="onsubmit">
			<view>提交</view>
		</view>
	</view>
</template>

<script>
	import baseHead from '@/pages/component/baseHead.vue'
	export default {
		data() {
			return {
				title: "修改绑定邮箱",
				type: null,
				form: {
					Email: "", //新邮箱
					oldEmail: '', //旧邮箱
					Code: '' //验证码
				},
				tips: '',
				disabled1: false,

			}
		},
		components: {
			baseHead,
		},
		onShow() {
			this.$util.currentUser().then(res => {
				this.type = uni.getStorageSync("homelist").UserInfo.Email;
				this.form.oldEmail = uni.getStorageSync("homelist").UserInfo.Email
			})
		},
		onLoad(option) {
			const app = getApp()
			this.type = app.globalData.user.Email
			this.form.oldEmail = app.globalData.user.Email
			if (!this.type) {
				this.title = "绑定邮箱"
			} else {
				this.title = "修改绑定邮箱"
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},


			yanzhengma() {

				let email_reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/;
				if (this.form.oldEmail == this.form.Email) {
					uni.$u.toast("新邮箱和原邮箱不能相等");
					return;
				}
				if (this.form.Email) {
					if (email_reg.test(this.form.Email)) {
						let obj = {
							"Email": this.form.Email,
							"Length": 4
						}
						console.log(obj);
						if (this.$refs.uCode.canGetCode) {
							uni.showLoading({
								title: '正在获取验证码'
							})
							this.$http.core.sendEmailCode(obj).then(res => {
								uni.hideLoading();
								if (res.Success) {
									uni.$u.toast(res.Message)
								}
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();

							})
						} else {
							uni.$u.toast('倒计时结束后再发送');
						}

					} else {
						uni.$u.toast("邮箱格式错误")
					}
				} else {
					uni.$u.toast("请输入邮箱")
				}

			},

			onsubmit() {

				let email_reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/;
				console.log(this.form.Code);


				if (!this.type) {
					if (!this.form.Code) {
						uni.$u.toast("请输入验证码")
						return;
					}
					if (!this.form.Email) {
						uni.$u.toast("请输入新邮箱")
						return;
					}

					if (email_reg.test(this.form.Email)) {
						let obj = {
							Code: this.form.Code,
							Email: this.form.Email,
							OldEmail: this.form.oldEmail
						}
						console.log(obj);
						this.$http.core.editEmail(obj).then(res => {
							console.log(res);
							if (res.Success) {
								uni.$u.toast(res.Message)
								this.$util.toast("绑定成功")
								setTimeout(() => {
									uni.navigateBack();
								}, 3000)
							}

						})
					} else {
						uni.$u.toast("邮箱错误")
					}
				} else {
					if (!this.form.oldEmail && !this.type == 1) {
						uni.$u.toast("请输入原邮箱")
						return;
					}
					if (!this.form.Code) {
						uni.$u.toast("请输入验证码")
						return;
					}
					if (!this.form.Email) {
						uni.$u.toast("请输入新邮箱")
						return;
					}


					if (this.form.oldEmail == this.form.Email) {
						uni.$u.toast("新邮箱和原邮箱不能相等");
						return;
					}
					if (email_reg.test(this.form.Email) && email_reg.test(this.form.oldEmail)) {
						let obj = {
							Code: this.form.Code,
							Email: this.form.Email,
							OldEmail: this.form.oldEmail
						}
						console.log(obj);
						this.$http.core.editEmail(obj).then(res => {
							console.log(res);
							if (res.Success) {
								uni.$u.toast(res.Message)
								this.$util.toast("修改成功")
								setTimeout(() => {
									uni.navigateBack();
								}, 1000)
							}

						})
					} else {
						uni.$u.toast("邮箱错误")
					}
				}

			}
		}
	}
</script>

<style scoped lang="scss">
	.box {
		margin: 20rpx 30rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		// border: 1px solid red;
		padding: 0rpx 40rpx;

		.content {
			// margin-top: 30rpx;
			background: #FFFFFF;
			display: flex;
			// align-items: center;
			justify-content: flex-start;

			border-bottom: 1rpx solid #E7E7E8;
		}
	}

	.btn {
		position: fixed;
		// width: 100%;
		bottom: 20rpx;
		right: 58rpx;
		left: 58rpx;
		background: #3476FE;
		border-radius: 10rpx 10rpx 10rpx 10rpx;
		opacity: 1;
		padding: 24rpx 288rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		view {
			font-size: 30rpx;
			font-weight: 400;
			color: #FFFFFF;
		}
	}
</style>